exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    /**
     * web端登录
     *
     * @return
     *result --用户信息 Object
     */
    checkLogin(username, password, params) {
        var _this = this;

        return _asyncToGenerator(function* () {
            const db = _this.db;
            const userModel = db['sys_user'];
            const encryptPsd = think.irreversibleEncrypt(username, password); //加密
            const nameSpace = think.config('nameSpace');
            try {
                // this.baseService.getRolePermissionList(['12313'])
                let userInfo = yield userModel.findOne({
                    where: {
                        username: username,
                        password: encryptPsd
                    },
                    attributes: { exclude: ['password'] }
                });
                if (userInfo) {
                    userInfo = userInfo.toJSON();
                    if (userInfo.login_flag === 0) {
                        throw '该账号已被锁定，请联系管理员';
                    } else if (userInfo.del_flag === 1) {
                        //逻辑删除
                        throw '该账号已被删除，请联系管理员恢复';
                    } else {

                        const administrator = think.config('administrator');
                        const userId = userInfo.id;
                        const orgId = userInfo.org_id;
                        const attributes = ['roleid'];
                        const where = { userid: userId };
                        const role = yield _this.CrudService.findAll('sys_role_user', where, attributes);
                        if (role.length === 0 && !administrator.includes(userId)) {
                            throw '该账号未分配角色和权限，请联系管理员';
                        } else {
                            // params.is_login = 1;  //将当前登录标记设为已登录
                            yield userModel.update(params, { where: { id: userId } });
                            userInfo.roleList = role.map(function (r) {
                                return r.roleid;
                            });
                            //获取用户的功能菜单
                            const menuList = yield _this.BaseService.getRoleMenu(userInfo.roleList, userId);
                            userInfo.menuList = menuList;
                            //把用户所有登陆信息缓存
                            think.Redis.hset(nameSpace.LOGIN_USER, userId, JSON.stringify(userInfo));
                            const tokenContent = {
                                userId: userId,
                                realName: userInfo.realname,
                                orgId: orgId,
                                roleList: userInfo.roleList
                            };
                            //获取token
                            const token = _this.BaseService.getToken(tokenContent);

                            return { userInfo, token };
                        }
                    }
                } else {
                    throw '用户名或密码输入错误';
                }
            } catch (err) {
                return _this.exceptionService.handleError(err);
            }
        })();
    }

    // //缓存用户信息
    // cacheUserInfo(userId, userInfo) {
    //     const nameSpace = think.config('nameSpace');
    //     const key = nameSpace.userInfo;
    //     think.Redis.set(`${key}:${userId}`, JSON.stringify(userInfo));
    // }


};